package cn.lili.modules.system.mapper;

import cn.lili.common.vo.PageVO;
import cn.lili.modules.goods.entity.dto.GoodsSearchParams;
import cn.lili.modules.goods.entity.vos.GoodsVO;
import cn.lili.modules.member.entity.vo.GoodsCollectionVO;
import cn.lili.modules.order.order.entity.dos.Order;
import cn.lili.modules.store.entity.dos.Store;
import cn.lili.modules.system.entity.dos.AppVersion;
import cn.lili.modules.system.entity.dos.Region;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 行政地区数据处理层
 *
 * @author Chopper
 * @since 2020/12/8 9:46
 */
public interface RegionMapper extends BaseMapper<Region> {


    /**
     * 根据区域查询区域的id
     */
    @Select(" select * from li_region  ${ew.customSqlSegment} ")
    Region getRegionId(@Param(Constants.WRAPPER) Wrapper<Region> queryWrapper);

    /**
     * 查询商品VO分页
     *
     * @param page         分页
     * @param queryWrapper 查询条件
     * @return 商品VO分页
     */
    @Select("select g.* from li_goods g left join li_store a  on a.id = g.store_id  ${ew.customSqlSegment}")
    IPage<GoodsVO> queryByParams(IPage<GoodsSearchParams> page, @Param(Constants.WRAPPER) Wrapper<GoodsSearchParams> queryWrapper);


    /**
     * 本区域销量前三的商品
     *
     * @return 商品VO分页
     */
    @Select("select g.* ,count(*) aa from li_store a  left join li_order o on a.id =  o.store_id " + " left join li_order_item oo on o.sn=oo.order_sn" + " left join li_goods g on g.id = oo.goods_id where a.store_address_id_path like concat('%', #{storeAddressIdPath}, '%') AND oo.id is not null GROUP BY oo.goods_id  ORDER BY aa desc limit #{sunm}")
    List<GoodsVO> getCommodity(String storeAddressIdPath, int sunm);

    /**
     * 全国
     *
     * @return 商品VO分页
     */
    @Select("select g.* ,count(*) aa from li_store a  left join li_order o on a.id =  o.store_id " + " left join li_order_item oo on o.sn=oo.order_sn" + " left join li_goods g on g.id = oo.goods_id where oo.id is not null GROUP BY oo.goods_id  ORDER BY aa desc limit #{sunm}")
    List<GoodsVO> getCommodityQg(int sunm);

    /**
     * 根据区域查询区域的id
     */
    @Select(" select * from li_region where level in('district','city','province') and ad_code = #{adCode}  ")
    Region getAdRegionId(String adCode);


    /**
     * 区域前14店铺
     */
    @Select("select a.*,COUNT(0) CC from li_store a  left join li_order o on a.id =  o.store_id  where a.store_address_id_path like concat('%', #{storeAddressIdPath}, '%') and a.id is not null and  store_disable='OPEN'    GROUP BY a.id  ORDER BY CC desc limit #{sunm}")
    List<Store> getCommodityShop(String storeAddressIdPath, int sunm);


    /**
     * 全国前14店铺
     */
    @Select("select a.*,COUNT(0) CC from li_store a  left join li_order o on a.id =  o.store_id  where a.id is not null and  store_disable='OPEN'   GROUP BY a.id  ORDER BY CC desc limit #{sunm}")
    List<Store> getCommodityShopQg(int sunm);
}